package com.doit.day05

object _03_剩下的算子 {
  def main(args: Array[String]): Unit = {
    val list: List[Int] = List(10, 20, 30, 40, 5,6)
    val list1: List[Int] = List(10, 20, 30, 40, 50)


    val iterator1: Iterator[List[Int]] = list.sliding(3, 2)
    while (iterator1.hasNext){
      println(iterator1.next())
    }

    val ints: List[Int] = list.slice(3, 6)
    println(ints)//List(30)  List(30, 40)  List(40, 5)


    val res10: List[(Int, Int)] = list.zip(list1)
    println(res10)//List((1,10), (2,20), (3,30), (4,40), (5,50))


    val index: List[(Int, Int)] = list.zipWithIndex

    println(index)










    //fold   折叠  做折叠
    val res5: Int = list.fold(10)((a: Int, b: Int) => {
      a - b
    })

    println(res5)//25

    val res6: Int = list.foldRight(10)((a: Int, b: Int) => {
      a - b
    })
    println(res6)//-7


    //reduce
    val res1: Int= list.reduce((a: Int, b: Int) => {
      //聚合的逻辑
      a + b
    })

    list.reduce((a:Int,b:Int)=>a+b)
    list.reduce(_+_)

    // 算的方式和我们正常人想法不一样
    val res3: Int = list.reduceRight((a: Int, b: Int) => {
      a - b
    })



    println(res3)






    val iterator: Iterator[List[Int]] = list.grouped(3)
    while (iterator.hasNext){
      println(iterator.next())
    }


    val tuples: List[(Int, String, Int, String)] = List((1, "zss", 18, "male"),
      (2, "lss", 28, "male"),
      (3, "ww", 58, "male"),
      (4, "ss", 48, "male"),
      (5, "zs", 38, "male"))


//    tuples.sortBy(-_._3).take(1).foreach(println)

    val tuple: (Int, String, Int, String) = tuples.maxBy(_._3)
    println(tuple)

    //mapValues 是针对key value结构的   也是做了一个map映射

    val map: Map[String, Int] = Map(("zss", 18), ("lss", 28), ("ww", 38))
    map.map(tp=>{
      (tp._1,tp._2*10)
    }).foreach(println)

    val res: Map[String, Int] = map.mapValues((a: Int) => a * 10)
    res.foreach(println)




  }
}
